package com.jspgou.cms.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;
import com.jspgou.cms.dao.ShipmentsDao;
import com.jspgou.cms.entity.Shipments;

@Repository
public class ShipmentsDaoImpl extends HibernateBaseDao<Shipments, Long> implements ShipmentsDao {
	public Pagination getPage(int pageNo, int pageSize) {
		Criteria crit = createCriteria();
		Pagination page = findByCriteria(crit, pageNo, pageSize);
		return page;
	}
	
	//前台发货订单列表页
	public Pagination getFcList(Long memberId,Long storeId,int pageNo,int pageSize){
		Finder f=Finder.create("from Shipments bean where 1=1");
		if(memberId!=null){	
			f.append(" and bean.indent.member.id=:memberId").setParam("memberId", memberId);
		}
		if(storeId!=null){
			f.append(" and bean.indent.store.id=:storeId").setParam("storeId", storeId);
		}
		f.append(" and bean.indent.shippingStatus=2");
		f.append(" order by bean.indent.createTime desc");
		return find(f,pageNo,pageSize);
	}
	
	public List<Shipments> getList(Long orderId){
		Finder f=Finder.create("from Shipments bean where bean.indent.id=:id");
		f.setParam("id", orderId);
		return find(f);
	}
	
	public Shipments findById(Long id) {
		Shipments entity = get(id);
		return entity;
	}

	public Shipments save(Shipments bean) {
		getSession().save(bean);
		return bean;
	}

	public Shipments deleteById(Long id) {
		Shipments entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	
	@Override
	protected Class<Shipments> getEntityClass() {
		return Shipments.class;
	}

	public Shipments findByorderId(Long orderId) {
		String hql="from Shipments bean where 1=1 and bean.indent.id=:orderId";
		Query query=getSession().createQuery(hql);
		query.setParameter("orderId", orderId);
		query.setMaxResults(1);
		return (Shipments) query.uniqueResult();
	}
}